package com.example.testcourse;

import java.util.Calendar;


import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;


public class Alarm extends Activity
{
    Button setTime1;
    AlarmManager aManager1;
    Button setTime2;
    AlarmManager aManager2;
    Calendar currentTime = Calendar.getInstance();

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm);
        // 获取程序界面的按钮
        setTime1 = (Button) findViewById(R.id.setTime1);
        // 获取AlarmManager对象
        aManager1 = (AlarmManager) getSystemService(
                Service.ALARM_SERVICE);
        // 为“设置闹铃”按钮绑定监听器。

        setTime2 = (Button) findViewById(R.id.setTime2);
        // 获取AlarmManager对象
        aManager2 = (AlarmManager) getSystemService(
                Service.ALARM_SERVICE);
        // 为“设置闹铃”按钮绑定监听器。




        setTime1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                Calendar currentTime = Calendar.getInstance();
                // 创建一个TimePickerDialog实例，并把它显示出来。
                new TimePickerDialog(Alarm.this, 0, // 绑定监听器
                        new TimePickerDialog.OnTimeSetListener()
                        {
                            @Override
                            public void onTimeSet(TimePicker tp,
                                                  int hourOfDay, int minute)
                            {
                                // 指定启动AlarmActivity组件
                                Intent intent = new Intent(Alarm.this,
                                        AlarmActivity.class);
                                // 创建PendingIntent对象
                                PendingIntent pi = PendingIntent.getActivity(
                                        Alarm.this, 0, intent, 0);
                                Calendar c = Calendar.getInstance();
                                // 根据用户选择时间来设置Calendar对象
                                c.set(Calendar.HOUR, hourOfDay);
                                c.set(Calendar.MINUTE, minute);
                                // 设置AlarmManager将在Calendar对应的时间启动指定组件
                                aManager1.set(AlarmManager.RTC_WAKEUP,
                                        c.getTimeInMillis(), pi);
                                // 显示闹铃设置成功的提示信息
                                Toast.makeText(Alarm.this, "上课闹铃设置成功"
                                        , Toast.LENGTH_SHORT).show();
                            }
                        }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime
                        .get(Calendar.MINUTE), false).show();
            }
        });


        setTime2.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                Calendar currentTime = Calendar.getInstance();
                // 创建一个TimePickerDialog实例，并把它显示出来。
                new TimePickerDialog(Alarm.this, 0, // 绑定监听器
                        new TimePickerDialog.OnTimeSetListener()
                        {
                            @Override
                            public void onTimeSet(TimePicker tp,
                                                  int hourOfDay, int minute)
                            {
                                // 指定启动AlarmActivity组件
                                Intent intent = new Intent(Alarm.this,
                                        AlarmActivity2.class);
                                // 创建PendingIntent对象
                                PendingIntent pi = PendingIntent.getActivity(
                                        Alarm.this, 0, intent, 0);
                                Calendar c = Calendar.getInstance();
                                // 根据用户选择时间来设置Calendar对象
                                c.set(Calendar.HOUR, hourOfDay);
                                c.set(Calendar.MINUTE, minute);
                                // 设置AlarmManager将在Calendar对应的时间启动指定组件
                                aManager2.set(AlarmManager.RTC_WAKEUP,
                                        c.getTimeInMillis(), pi);
                                // 显示闹铃设置成功的提示信息
                                Toast.makeText(Alarm.this, "交作业闹铃设置成功"
                                        , Toast.LENGTH_SHORT).show();
                            }
                        }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime
                        .get(Calendar.MINUTE), false).show();
            }
        });


    }
}